<template>
  <div class="vue2-app">
    <h2>Vue2 子应用</h2>
    <div class="counter-container">
      <p>计数器: {{ counter }}</p>
      <button @click="increment">增加</button>
      <button @click="decrement">减少</button>
    </div>
    <div class="todo-list">
      <h3>待办事项</h3>
      <div class="add-todo">
        <input
          v-model="newTodo"
          placeholder="输入新待办事项"
          @keyup.enter="addTodo"
        />
        <button @click="addTodo">添加</button>
      </div>
      <ul>
        <li v-for="(todo, index) in todos" :key="index">
          <input type="checkbox" v-model="todo.completed" />
          <span :class="{ completed: todo.completed }">{{ todo.text }}</span>
          <button @click="removeTodo(index)">删除</button>
        </li>
      </ul>
    </div>

    <!-- 添加 Vue3 表格页面展示区域 -->
    <div class="vue3-table-container">
      <h3>Vue3 表格页面</h3>
      <WujieVue
        width="100%"
        height="600px"
        name="vue3-table"
        url="http://localhost:8083/"
        :props="{
          path: '/table'
        }"
        :sync="true"
      ></WujieVue>
    </div>

    <div class="message-box">
      <h3>与 Vue3 通信</h3>
      <input v-model="message" placeholder="输入要发送的消息" />
      <button @click="sendToVue3">发送到 Vue3</button>
      <p v-if="receivedMsg">收到消息: {{ receivedMsg }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      counter: 0,
      newTodo: "",
      todos: [],
      message: "",
      receivedMsg: "",
    };
  },
  created() {
    // 监听来自 Vue3 子应用的消息
    window.$wujie?.bus.$on("vue3-to-vue2", (data) => {
      this.receivedMsg = data;
    });
  },
  methods: {
    increment() {
      this.counter++;
    },
    decrement() {
      this.counter--;
    },
    addTodo() {
      if (this.newTodo.trim()) {
        this.todos.push({
          text: this.newTodo,
          completed: false,
        });
        this.newTodo = "";
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    sendToVue3() {
      window.$wujie?.bus.$emit("vue2-to-vue3", this.message);
      this.message = "";
    },
  },
};
</script>

<style>
.vue2-app {
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.counter-container {
  margin: 20px 0;
  padding: 15px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.todo-list {
  background: white;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.add-todo {
  margin-bottom: 15px;
  display: flex;
  gap: 10px;
}

.add-todo input {
  flex: 1;
  padding: 5px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  background-color: #4caf50;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  margin: 0 5px;
}

button:hover {
  background-color: #45a049;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  display: flex;
  align-items: center;
  padding: 8px;
  border-bottom: 1px solid #eee;
}

li span {
  margin: 0 10px;
  flex: 1;
}

.completed {
  text-decoration: line-through;
  color: #888;
}

.vue3-table-container {
  margin: 20px 0;
  padding: 15px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.vue3-table-container h3 {
  margin-bottom: 15px;
}
</style>
